#__*__coeding:utf-8__*__
#!/usr/share/python
'''
---------------------------------------------------------------- 
 版权所有。  
 
 文件名：
 文件功能描述： 
 author：
 时间：
--------------------------------------------------------------- 
'''

from collections import Counter
import socket
from Base.external_interface import request_query_ip
from Base.tools import tools


class UserAnalysis():

    def repeat_statistical(self,*args):
        counter=Counter(*args)
        counter_statis=counter.most_common(10)
        return counter_statis


    def all_or_date(self,txt_list):
        allcunt=txt_list['allcunt']
        log_dates=txt_list['log_date']
        logforwardhost = socket.gethostname()
        all_or_date=(allcunt,log_dates,logforwardhost)
        return all_or_date

    def accsee_ip(self,txt_list):
        accsee_remot=[]
        IP_LIST=txt_list['IP']
        IP_STATISTICAL=self.repeat_statistical(self,IP_LIST)
        try:
            for i in IP_STATISTICAL:
                s_IP={'s_IP':i[0]}
                s_PV={'s_PV':i[1]}
                ip_api_result=request_query_ip.req_ip_api(request_query_ip,i[0])
                ip_api_result.update(s_IP)
                ip_api_result.update(s_PV)
                accsee_remot.append(ip_api_result)
            return accsee_remot
        except Exception as e:
            return None

    def Access_time(self,txt_list):
        return self.repeat_statistical(self,txt_list["Access_time"])

    def Access_referer_addr(self,txt_list):
        return self.repeat_statistical(self,txt_list["Access_referer_addr"])

    def Access_agent(self,txt_list):
        return self.repeat_statistical(self,txt_list["Access_agent"])

    def Access_forwarded_for(self,txt_list):
        forwarded=txt_list["Access_forwarded_for"]
        redefinition_forwarded=self.Device_type_redefinition(self,forwarded)
        redefinition_forwarded=tools.sorted(self,redefinition_forwarded)
        return redefinition_forwarded

    def Device_type_redefinition(self,forwarded):
        amountWindows = 0
        amountAndroid = 0
        amountIPhone = 0
        amountMAC = 0
        WIN = 'Windows'
        AND = 'Android'
        IPH = 'iPhone'
        MAC = 'Mac'
        new_forwarded = []
        for j in forwarded:
            if WIN in j:
                amountWindows += 1
            elif AND in j:
                amountAndroid += 1
            elif IPH in j:
                amountIPhone += 1
            elif MAC in j:
                amountMAC += 1
            else:
                new_forwarded.append(j)
        new_forwarded = self.repeat_statistical(self, new_forwarded)
        new_forwarded.append((WIN, amountWindows))
        new_forwarded.append((AND, amountAndroid))
        new_forwarded.append((IPH, amountIPhone))
        new_forwarded.append((MAC, amountMAC))
        return new_forwarded



